HMathParser = require('HMathParser')
local function split(input, delimiter)
	input = tostring(input)
	delimiter = tostring(delimiter)
	if (delimiter == "") then return false end
	local pos, arr = 0, {}
	for st, sp in function() return string.find(input, delimiter, pos, true) end do
		table.insert(arr, string.sub(input, pos, st - 1))
		pos = sp + 1
	end
	table.insert(arr, string.sub(input, pos))
	return arr
end

function Calca_Translator(input, seg, env)
	if not input:match("^/js") then return end
	if not input:match("=") then return end
	if string.match(input, '^/js.+') then
		local num = split(string.sub(input, 4), '=')[1]
		if num == nil then return end
		local result = load("return " .. num)()
		-- local result = HMathParser.Compute(" "..num)
		if type(result) == 'number' then 
		yield(Candidate("calca", seg.start, seg._end, result, ""))
		else
		yield(Candidate("calca", seg.start, seg._end, "计算公式或者计算出错", ""))
		end

	end
end

function Calca_Tips()
	return "/js .+? 计算器 "
end
